package com.common.oss;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectResult;
import com.common.utils.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;


/**
 * 2018/5/3 update
 * OSS文件上传Client (Singleton Pattern)
 */
public class OssManager {

    private static Logger logger = LoggerFactory.getLogger(OssManager.class);
    /* 文件分类 */
    public static final String VIDEO = "video";
    public static final String AUDIO = "audio";
    public static final String PICTURE = "picture";
    public static final String DOCUMENT = "document";

    /* OSS配置 */
    public static String endpoint;
    public static String accessKeyId;
    public static String secretAccessKey;
    public static String bucketName;
    private OSSClient client;

    private static OssManager instance = new OssManager();

    public static OssManager getInstance() {
        return instance;
    }

    public OssManager() {
        //TODO
        endpoint = "oss-cn-hangzhou.aliyuncs.com";
        accessKeyId = "LTAIo0J0qCxwEoiC";
        secretAccessKey = "F9RlOf1hcNFmiopNmU3VEm7hjVky24";
        bucketName = "app-story-test";
        client = new OSSClient(endpoint, accessKeyId, secretAccessKey);
    }


    public String uploadFile(String fileName, InputStream input) {
        try {
            String Suffix = FileUtils.getFileSuffixWithPoint(fileName);
            String fullPath = String.format("%s/%s", Suffix, fileName);//windows系统和linux系统都固定为/
            System.out.println("---->文件名:" + fullPath);
            PutObjectResult result = client.putObject(bucketName, fullPath, input);
            result.getCallbackResponseBody();
            return String.format("http://%s.%s/%s", bucketName, endpoint, fullPath);
        } catch (OSSException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } finally {
            try {
                if (input != null) input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    /**
     * 上传文件
     *
     * @param fileName
     * @param file
     * @param uploadPath
     * @return URL
     */
    public String uploadFile(String fileName, File file, String uploadPath) {
        String fullPath = String.format("%s/%s", uploadPath, fileName);
        if (logger.isDebugEnabled()) {
            logger.info("====>upload " + uploadPath + " " + fullPath);
        }
        try {
            PutObjectResult result = client.putObject(bucketName, fullPath, file);
            return String.format("http://%s.%s/%s", bucketName, endpoint, fullPath);
        } catch (OSSException e) {
            logger.debug("oss上传" + uploadPath + "失败", e);
            logger.error("oss上传" + uploadPath + "失败", e);
        } catch (ClientException e) {
            logger.debug("oss上传" + uploadPath + "失败", e);
            logger.error("oss上传" + uploadPath + "失败", e);
        }
        return null;

    }


//    public static void main(String[] args) {
//        File f = null;
//        try {
//            f = new File("c:/test.jpg");
//            OssManager.getInstance().uploadFile("test.jpg", f, "picture");
//            System.out.println("fileName : " + f.getName());
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//    }
}

